﻿using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anke.SHManage.MSSQLDAL
{
    public class TaskAmbulanceDAL
    {
        public TaskAmbulanceInfo GetTaskAmbulance(string taskCode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@" 
select 操作来源编码,姓名=isnull(姓名,''),车辆状态编码,时刻值  
into #temp  
from TAmbulanceStateTime ttime  
left join dbo.TPerson tp on ttime.操作员编码=tp.编码 
where ttime.任务编码='" + taskCode + @"'
order by 记录时刻 desc

select tt.任务编码,事件编码,受理序号,tt.任务流水号,tt.用户流水号
,责任调度人编码,p.姓名 as 责任调度人姓名,tt.分站编码,ts.名称 as 分站名称
,tt.车辆编码,tab.车牌号码,tab.实际标识,tab.随车电话,tab.车辆类型编码,tzat.名称 as 车辆类型名称
,tt.是否执行中,生成任务时刻
--,接收命令时刻1=tt.接收命令时刻,出车时刻1=tt.出车时刻,到达现场时刻1=tt.到达现场时刻,离开现场时刻1=tt.离开现场时刻
--,到达医院时刻1=tt.到达医院时刻,完成时刻1=tt.完成时刻,返回站中时刻1=tt.返回站中时刻
,急救公里数,行驶公里数,是否正常结束,异常结束原因编码,isnull(tztr.名称,'')  as 异常结束原因
,备注,tt.实际救治人数,tt.是否站内出动,tt.司机,tt.医生,tt.护士,tt.担架工,tt.抢救员
,tt.改派前任务编码,tt.实际送往地点,tt.备注,tt.中心编码,中心名称=tc.名称  
,接收命令时刻 =Convert(varchar(19),tt.接收命令时刻,21)+'('+(select top 1 操作来源=case 操作来源编码 when 1 then '台:'+ t.姓名 when 2 then '车载' when 5 then '管理系统' +t.姓名 end from #temp t where t.时刻值=tt.接收命令时刻 and t.车辆状态编码=1)+')'
,出车时刻 = Convert(varchar(19),tt.出车时刻,21)+'('+(select top 1 操作来源=case 操作来源编码 when 1 then '台:'+ t.姓名 when 2 then '车载' when 5 then '管理系统' +t.姓名 end from #temp t where t.时刻值=tt.出车时刻 and t.车辆状态编码=2)+')'
,到达现场时刻 = Convert(varchar(19),tt.到达现场时刻,21)+'('+(select top 1 操作来源=case 操作来源编码 when 1 then '台:'+ t.姓名 when 2 then '车载' when 5 then '管理系统' +t.姓名 end from #temp t where t.时刻值=tt.到达现场时刻 and t.车辆状态编码=3)+')'
,离开现场时刻 = Convert(varchar(19),tt.离开现场时刻,21)+'('+(select top 1 操作来源=case 操作来源编码 when 1 then '台:'+ t.姓名 when 2 then '车载' when 5 then '管理系统' +t.姓名 end from #temp t where t.时刻值=tt.离开现场时刻 and t.车辆状态编码=4)+')'
,到达医院时刻 =  Convert(varchar(19),tt.到达医院时刻,21)+'('+(select top 1 操作来源=case 操作来源编码 when 1 then '台:'+ t.姓名 when 2 then '车载' when 5 then '管理系统' +t.姓名 end from #temp t where t.时刻值=tt.到达医院时刻 and t.车辆状态编码=5)+')'
,完成时刻 =  Convert(varchar(19),tt.完成时刻,21)+'('+(select top 1 操作来源=case 操作来源编码 when 1 then '台:'+ t.姓名 when 2 then '车载' when 5 then '管理系统' +t.姓名 end from #temp t where t.时刻值=tt.完成时刻 and t.车辆状态编码=6)+')'
,返回站中时刻 = Convert(varchar(19),tt.返回站中时刻,21)+'('+(select top 1 操作来源=case 操作来源编码 when 1 then '台:'+ t.姓名 when 2 then '车载' when 5 then '管理系统' +t.姓名 end from #temp t where t.时刻值=tt.返回站中时刻 and t.车辆状态编码=7)+')'
,司机编码=dbo.GetCode(tt.任务编码,3),医生编码=dbo.GetCode(tt.任务编码,4),护士编码=dbo.GetCode(tt.任务编码,5)
,担架工编码=dbo.GetCode(tt.任务编码,6),抢救员编码=dbo.GetCode(tt.任务编码,7)
from TTask  tt
left join TAmbulance  tab on tt.车辆编码 = tab.车辆编码
left join TZAmbulanceType tzat on tzat.编码 = tab.车辆类型编码
left join TStation ts on ts.编码 = tt.分站编码
left join TCenter tc on tc.编码 = tt.中心编码
left join TPerson p on p.编码  = tt.责任调度人编码
left join  TZTaskAbendReason tztr on tt.异常结束原因编码 = tztr.编码
where tt.任务编码='" + taskCode + @"'
drop table #temp ");
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null))
            {
                TaskAmbulanceInfo TAinfo = new TaskAmbulanceInfo();
                if (dr.Read())
                {
                    TAinfo.TaskCode = Convert.ToString(dr["任务编码"]);
                    TAinfo.EventCode = Convert.ToString(dr["事件编码"]);
                    TAinfo.AcceptNumber = Convert.ToInt16(dr["受理序号"]);
                    TAinfo.TaskStreamNumber = Convert.ToString(dr["任务流水号"]);
                    TAinfo.UserFlowNumber = Convert.ToString(dr["用户流水号"]);

                    TAinfo.DispatcherCode = Convert.ToString(dr["责任调度人编码"]);
                    TAinfo.DispatcherName = Convert.ToString(dr["责任调度人姓名"]);
                    TAinfo.StationCode = Convert.ToString(dr["分站编码"]);
                    TAinfo.StationName = Convert.ToString(dr["分站名称"]);
                    TAinfo.AmbulanceCode = Convert.ToString(dr["车辆编码"]);
                    TAinfo.AmbulanceNumber = Convert.ToString(dr["车牌号码"]);
                    TAinfo.RealAmbulanceSign = Convert.ToString(dr["实际标识"]);
                    TAinfo.AmbulanceTel = Convert.ToString(dr["随车电话"]);
                    TAinfo.AmbulanceTypeCode = Convert.ToString(dr["车辆类型编码"]);
                    TAinfo.AmbulanceType = Convert.ToString(dr["车辆类型名称"]);

                    TAinfo.IFCarryout = Convert.ToBoolean(dr["是否执行中"]);
                    TAinfo.StartTaskTime = DBConvert.ConvertNullableToNullableTime(dr["生成任务时刻"].ToString());
                    TAinfo.GetCommandTime = (dr["接收命令时刻"].ToString());
                    TAinfo.OutAmbulanceTime =(dr["出车时刻"].ToString());
                    TAinfo.ArriveSenceTime = (dr["到达现场时刻"].ToString());
                    TAinfo.LeaveSenceTime = (dr["离开现场时刻"].ToString());
                    TAinfo.ArriveHospitalTime = (dr["到达医院时刻"].ToString());
                    TAinfo.MissionCompleteTime = (dr["完成时刻"].ToString());
                    TAinfo.ReturnStationTime =(dr["返回站中时刻"].ToString());
                    //TAinfo.TGetCommandTime = Convert.ToDateTime(dr["接收命令时刻1"].ToString());
                    //TAinfo.TOutAmbulanceTime = (dr["出车时刻1"].ToString());
                    //TAinfo.TArriveSenceTime = (dr["到达现场时刻1"].ToString());
                    //TAinfo.TLeaveSenceTime = (dr["离开现场时刻1"].ToString());
                    //TAinfo.TArriveHospitalTime = (dr["到达医院时刻1"].ToString());
                    //TAinfo.TMissionCompleteTime = (dr["完成时刻1"].ToString());
                    //TAinfo.TReturnStationTime = (dr["返回站中时刻1"].ToString());
                    TAinfo.EmergencyKM = Convert.ToString(dr["急救公里数"]);
                    TAinfo.DriveKM = Convert.ToString(dr["行驶公里数"]);
                    TAinfo.IFNormalEnd = Convert.ToBoolean(dr["是否正常结束"]);
                    TAinfo.ResultCode = Convert.ToString(dr["异常结束原因编码"]);
                    TAinfo.ResultReason = Convert.ToString(dr["异常结束原因"]);
                    TAinfo.PatientCount = Convert.ToString(dr["实际救治人数"]);
                    TAinfo.IsOutFromStation = Convert.ToBoolean(dr["是否站内出动"]);

                    TAinfo.Driver = Convert.ToString(dr["司机"]);
                    TAinfo.Doctor = Convert.ToString(dr["医生"]);
                    TAinfo.Nurse = Convert.ToString(dr["护士"]);
                    TAinfo.DanJiaGong = Convert.ToString(dr["担架工"]);
                    TAinfo.Rescue = Convert.ToString(dr["抢救员"]);
                    TAinfo.ForeTaskID = Convert.ToString(dr["改派前任务编码"]);
                    TAinfo.FactAddr = Convert.ToString(dr["实际送往地点"]);                   
                    TAinfo.TaskRemark = Convert.ToString(dr["备注"]);
                    TAinfo.CenterCode = Convert.ToString(dr["中心编码"]);
                    TAinfo.CenterName = Convert.ToString(dr["中心名称"]);
                    TAinfo.DriverCodes = Convert.ToString(dr["司机编码"]);
                    TAinfo.DoctorCodes = Convert.ToString(dr["医生编码"]);
                    TAinfo.NurseCodes = Convert.ToString(dr["护士编码"]);
                    TAinfo.LitterCodes = Convert.ToString(dr["担架工编码"]);
                    TAinfo.RescueCodes = Convert.ToString(dr["抢救员编码"]);
                }
                return TAinfo;

            }
        }
        //取绿色通道
        public TaskGreenDate GetTaskGreenDate(string taskCode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"
                select 患者姓名,拟诊,送往医院,联系电话,预报人,登录人,时间,备注
                    from TGreenDate  where 任务编码='" + taskCode + @"' ");

            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null))
            {
                TaskGreenDate TAinfo = new TaskGreenDate();
                if (dr.Read())
                {
                    TAinfo.PatientName = Convert.ToString(dr["患者姓名"]);
                    TAinfo.NiZhen = Convert.ToString(dr["拟诊"]);
                    TAinfo.SongWangYiYuan = Convert.ToString(dr["送往医院"]);
                    TAinfo.LianXiDianHua = Convert.ToString(dr["联系电话"]);
                    TAinfo.YuBaoRen = Convert.ToString(dr["预报人"]);
                    TAinfo.DengLuRen = Convert.ToString(dr["登录人"]);
                    TAinfo.ShiJian = Convert.ToString(dr["时间"]);
                    TAinfo.GreenRemark = Convert.ToString(dr["备注"]);

                }
                return TAinfo;
            }
        }


        //根据事件编码和受理次数判断有几次任务 
        public int getTaskTime(string eventCode, int orderNumber)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"  select 
                        count(tt.任务编码) as 几次
                       
                        from TTask tt 
                        where tt.事件编码='" + eventCode + @"' and 受理序号=" + orderNumber + " ");
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null))
            {
                int result = 0;
                if (dr.Read())
                {
                    result = Convert.ToInt16(dr["几次"]);
                }
                return result;
            }
        }

        //根据事件编码和受理次数取出任务编码，可能会出现一次受理多次任务的情况(有大于等于1个的任务编码)，所有这里返回使用List<string> 
        public List<string> getTaskCode(string eventCode, int orderNumber)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"
                        select 
                        tt.任务编码
                        from TTask tt 
                    where tt.事件编码='" + eventCode + @"' and 受理序号=" + orderNumber + " ");
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null))
            {
                List<string> ls = new List<string>();
                while (dr.Read())
                {
                    CheckModelExt cm = new CheckModelExt();
                    cm.Name = Convert.ToString(dr["任务编码"]);
                    ls.Add(cm.Name);
                }
                return ls;
            }

        }
        //根据事件编码和受理序号取出有任务的受理序号字符串,使用list<string>  (母页面用来默认选中使用)
        public List<string> getHadTaskOrder(string eventCode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"
                        select 
                       受理序号
                        from TTask tt 
                    where tt.事件编码='" + eventCode + @"'");
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null))
            {
                List<string> ls = new List<string>();
                if (dr.Read())
                {
                    CheckModelExt cm = new CheckModelExt();
                    cm.Name = Convert.ToString(dr["受理序号"]);
                    ls.Add(cm.Name);
                }
                return ls;
            }

        }

//        public zTelRecordInfo GetTelRecordInfo(string TelRecordCode) //add2021-05-26 plq 移植奉贤代码，适配V8无TTelRocord
//        {
//            StringBuilder sb = new StringBuilder();
//            sb.Append(@"
//            select 产生时刻,tac.主叫号码,呼入时刻,排队时刻,震铃时刻,tr.通话时刻 ,tr.结束时刻,调度员工号 ,p.姓名 as 调度员姓名,tr.录音号,tr.台号, tzrt.名称 as 记录类型,tzlo.名称 as 操作说明,tzlr.名称 as 结果类型,tzact.名称 as 通话类型 from TTelLog tr left join  TPerson p  on  tr.调度员工号 =p.编码
//            left join TZTelLogRecordType tzrt on tzrt.编码 = tr.记录类型编码
//            left join TZTelLogOperator tzlo on tzlo.编码 = tr.操作说明编码
//            left join TZTelLogResult tzlr on tzlr.编码 = tr.结果编码
//            left join TAlarmCall tac on tac.录音号=tr.录音号
//            left join TZAlarmCallType tzact on tac.通话类型编码 = tzact.编码
//                where tr.编码='" + TelRecordCode + @"'");
//            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null))
//            {
//                zTelRecordInfo TAinfo = new zTelRecordInfo();
//                if (dr.Read())
//                {
//                    TAinfo.CallNumber = Convert.ToString(dr["主叫号码"]);
//                    TAinfo.TeskCode = Convert.ToString(dr["台号"]);
//                    TAinfo.DiaoDuWorkCode = Convert.ToString(dr["调度员工号"]);
//                    TAinfo.DiaoDuName = Convert.ToString(dr["调度员姓名"]);

//                    TAinfo.AlarmTime = DBConvert.ConvertDBTypeToNullable(dr["产生时刻"]);
//                    TAinfo.InListTime = DBConvert.ConvertDBTypeToNullable(dr["排队时刻"]);
//                    TAinfo.OutListTime = DBConvert.ConvertDBTypeToNullable(dr["呼入时刻"]);
//                    TAinfo.TelAlarmTime = DBConvert.ConvertDBTypeToNullable(dr["震铃时刻"]);
//                    TAinfo.CallStartTime = DBConvert.ConvertDBTypeToNullable(dr["通话时刻"]);
//                    //TAinfo.MeetTime = DBConvert.ConvertDBTypeToNullable(dr["会议时刻"]);
//                    //TAinfo.TransTime = DBConvert.ConvertDBTypeToNullable(dr["转移时刻"]);
//                    TAinfo.EndTime = DBConvert.ConvertDBTypeToNullable(dr["结束时刻"]);
//                    TAinfo.RecordNumber = Convert.ToString(dr["录音号"]);
//                    TAinfo.EndType = Convert.ToString(dr["结果类型"]);
//                    TAinfo.InListNumber = Convert.ToString(dr["通话类型"]);
//                    TAinfo.IfCallOut = Convert.ToString(dr["记录类型"]);
//                    TAinfo.IfWireless = Convert.ToString(dr["操作说明"]);


//                }
//                return TAinfo;
//            }
//        }

        /// <summary>
        /// add2021-07-02 plq
        /// 移植上海FAWeb V8电话流水详情取值
        /// </summary>
        /// <param name="TelRecordCode"></param>
        /// <returns></returns>
        public zTelRecordInfo GetTelRecordInfo(string TelRecordCode)  //2020-11-22 zch 需要改成TelLog
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"
            select 
                  对方电话
                  ,tzrt.名称 as 记录类型 
                  ,呼入时刻 
                  ,排队时刻
                  ,震铃时刻
                  ,通话时刻
                  ,结束时刻
                  ,中间操作时刻
                  ,中间操作号码
                  ,调度员工号 = p.工号
                  ,p.姓名 as 调度员姓名
                  ,录音号
                  ,台号,tzo.名称 as 操作说明
                  ,tztr.名称 as 结果类型
                  from TTelLog tr 
                  left join  TPerson p  on  tr.调度员工号 =p.编码
                  left join TZTelLogRecordType tzrt on tzrt.编码 = tr.记录类型编码
                  left join TZTelLogResult tztr on tztr.编码 = tr.结果编码
                  left join TZTelLogOperator tzo on tzo.编码 = tr.操作说明编码
                where tr.编码='" + TelRecordCode + @"'");
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null))
            {
                zTelRecordInfo TAinfo = new zTelRecordInfo();
                if (dr.Read())
                {
                    TAinfo.CallNumber = Convert.ToString(dr["对方电话"]);
                    TAinfo.TelType = Convert.ToString(dr["记录类型"]);
                    TAinfo.AlarmTime = DBConvert.ConvertDBTypeToNullable(dr["呼入时刻"]);
                    TAinfo.InListTime = DBConvert.ConvertDBTypeToNullable(dr["排队时刻"]);
                    TAinfo.TelAlarmTime = DBConvert.ConvertDBTypeToNullable(dr["震铃时刻"]);
                    TAinfo.CallStartTime = DBConvert.ConvertDBTypeToNullable(dr["通话时刻"]);
                    TAinfo.EndTime = DBConvert.ConvertDBTypeToNullable(dr["结束时刻"]);
                    TAinfo.MeetTime = DBConvert.ConvertDBTypeToNullable(dr["中间操作时刻"]);
                    TAinfo.InListNumber = dr["中间操作号码"].ToString();
                    TAinfo.DiaoDuWorkCode = Convert.ToString(dr["调度员工号"]);
                    TAinfo.DiaoDuName = Convert.ToString(dr["调度员姓名"]);
                    TAinfo.RecordNumber = Convert.ToString(dr["录音号"]);
                    TAinfo.TeskCode = Convert.ToString(dr["台号"]);
                    TAinfo.EndType = Convert.ToString(dr["结果类型"]);
                    TAinfo.Operator = Convert.ToString(dr["操作说明"]);
                    //TAinfo.OutListTime = DBConvert.ConvertDBTypeToNullable(dr["出队列时刻"]);   
                    //TAinfo.TransTime = DBConvert.ConvertDBTypeToNullable(dr["转移时刻"]);
                    //TAinfo.IfCallOut = Convert.ToBoolean(dr["是否呼出"]);
                    //TAinfo.IfWireless = Convert.ToBoolean(dr["是否无线"]);
                }
                return TAinfo;
            }
        }
    }
}
